<?php
class AdsParser{

	public static function Instance(){
		return new AdsParser();
	}


	public function parseAdResultToXml($result){
		if ($result instanceof Model_Ads){
			$entity = $this->parseSupplier($result);
		}else{
			$result_array = array();
			foreach ($result as $item){
				array_push($result_array, $this->parseAds($item));
			}
			$entity = new Entity();
			$entity->entityName = "Ads";
			$entity->entityValue = $result_array;
		}

		$xml = XmlUtil::Factory()->entityToXml($entity,0,0);
		return $xml;
	}

	private function parseAds($item){
		$itemArray['Id'] = $item->id;
		$itemArray['Type'] = $item->type;
		$itemArray['Value'] = $item->value;
		if (!empty($item->picture)){
			if (file_exists(realpath($this->defaultImgPath().$item->picture))){
				$itemArray['ImgUrl'] = URL::domain().ADS_IMG_PATH.$item->picture;
			}else{
				$itemArray['ImgUrl'] = "";
			}
		}else{
			$itemArray['ImgUrl'] = "";
		}
		$itemArray['Position'] = $item->position;
		$itemArray['Sort'] = $item->sort;
		$itemArray['Desc'] = $item->desc;
		$itemArray['CreateTime'] = $item->createtime;
		$itemArray['UpdateTime'] = $item->updatetime;
		$entity = new Entity();
		$entity->entityName = "Ad";
		$entity->entityValue = $itemArray;
		return $entity;
	}
	
	private function defaultImgPath(){
		return $_SERVER["DOCUMENT_ROOT"].DIRECTORY_SEPARATOR.Upload::$default_directory.DIRECTORY_SEPARATOR.'ads'.DIRECTORY_SEPARATOR;
	}
}